// pages/work/work.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 0,
    pageSize: 10,
    isScorll: false,
    isAdd: false,
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // console.log('onShow');
    
    if(!wx.getStorageSync('loginDate')){
      Dialog.alert({
        message: '未登录，请先登录',
        theme: 'round-button',
      })
        .then(() => {
          wx.reLaunch({
            url: '/pages/mine/mine'
          })
        })
        return
    }else {
      if(wx.getStorageSync('userInfo') && JSON.parse(wx.getStorageSync('userInfo')).type != 'user'){
        this.setData({
          isAdd: true
        })
      }else {
        this.setData({
          isAdd: false
        })
      }
      this.setData({
        list: [],
        type: JSON.parse(wx.getStorageSync('userInfo')).type
      }, ()=>{this.getList()})
    }
  },
  addwork() {
    if(!wx.getStorageSync('loginDate')){
      Dialog.alert({
        message: '未登录，请先登录',
        theme: 'round-button',
      })
        .then(() => {
          wx.reLaunch({
            url: '/pages/mine/mine'
          })
        })
    }else {
      wx.navigateTo({
        url: '/packageA/pages/addWork/addWork?isWork=' + true,
      })
    }
  },
  getList(){
    this.setData({isScorll: false})
    let that = this
    db.collection('pastCases').orderBy('date', 'desc').limit(that.data.pageSize).skip(that.data.pageNum).get().then(res=>{
      let oldData = that.data.list
      let newData = oldData.concat(res.data)
      if(res.data.length != 0){
        that.setData({
          isScorll: true
        })
      }
      that.setData({
        list: newData
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('onPullDownRefresh');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },
  bindscrolltolower(){
    // console.log(this.data.isScorll);
    if(this.data.isScorll){
      let pageNum = this.data.pageNum + this.data.pageSize
      this.setData({
        pageNum: pageNum,
        pageSize: this.data.pageSize
      }, ()=>{this.getList()})
    }
   
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})